package com.cccyy.http;

import org.apache.commons.codec.binary.Hex;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class HttpTimeOut {
    private static final Logger logger = LoggerFactory.getLogger(HttpTimeOut.class);

    public static void main(String[] args) {
        StringBuilder stBder = new StringBuilder("shdasf");
        stBder.append("&spid=" + "dsfhoshf");
        stBder.append("&sppassword=" + "dfhsldkkfhls");
        String msg = "您的验证码为";
        String msg1 = Hex.encodeHexString(msg.getBytes(StandardCharsets.UTF_16BE));
        stBder.append("&sm=" + msg1);
        String str = stBder.toString();
//            http://mlinkhost/sms/mt?command=MT_REQUEST&spid=****&sppassword=****&da=8613812345678&dc=8&sm=4f60597d0032003000300038
        logger.info("短信验证码发送的请求: " + str);
        String result = "";
        HttpHost proxy = new HttpHost("proxyIp", 8202, "HTTP");
        RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(1000)
                .setSocketTimeout(5000).setProxy(proxy).build();
        HttpGet request = new HttpGet(str);
        request.setConfig(config);
        CloseableHttpClient httpclient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpEntity entity = response.getEntity();
        //将返回实体转化为输入流
        InputStream is = null;
        try {
            is = entity.getContent();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //读取输入流，即返回文本内容
        StringBuffer sb = new StringBuffer();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = "";
        while (true) {
            try {
                if (!((line = br.readLine()) != null)) {
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            sb.append(line);
        }
        try {
            is.close();
            br.close();
            response.close();
            httpclient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        result = sb.toString();
    }


    public static String testTimeout(String url) {
        // 设置HTTP请求参数
        String result = null;
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(50000).setConnectionRequestTimeout(10000)
                .setSocketTimeout(50000).build();
        httpGet.setConfig(requestConfig);
        try {
            CloseableHttpResponse response = client.execute(httpGet);
            result = EntityUtils.toString(response.getEntity(), "UTF-8");
        } catch (ClientProtocolException e) {
            logger.error("http接口调用异常：url is::" + url, e);
            return null;
        } catch (Exception e) {
            logger.error("http接口调用异常：url is::" + url, e);
            return null;
        } finally {
            try {
                client.close();
            } catch (IOException e) {
                logger.error("http接口调用异常：url is::" + url, e);
            }
        }
        return result;
    }
}


